package com.finger.forest.utils;

import android.app.Activity;
import android.content.Context;
import android.content.res.AssetManager;
import android.util.DisplayMetrics;
import android.util.Log;
import android.view.WindowManager;

import androidx.fragment.app.FragmentActivity;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.esri.arcgisruntime.data.TileCache;
import com.esri.arcgisruntime.layers.ArcGISTiledLayer;
import com.esri.arcgisruntime.mapping.ArcGISMap;
import com.esri.arcgisruntime.mapping.Basemap;
import com.finger.forest.CollectionFireActivity;
import com.finger.forest.base.CommonKey;
import com.finger.forest.base.SystemConfig;
import com.finger.forest.model.BaseModel;
import com.finger.forest.model.ResourceTypeModel;
import com.finger.forest.utils.encode.PrefsUtil;
import com.google.gson.JsonObject;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.List;

public class Utils {

    private static final String TAG = "Utils";

    //获取屏幕宽度
    public static int getScreenWidth(Context context){
        //获取窗口管理服务
        WindowManager wm = (WindowManager) context.getSystemService(Context.WINDOW_SERVICE);

        //屏幕参数对象
        DisplayMetrics outMetrics = new DisplayMetrics();

        //获取屏幕参数
        wm.getDefaultDisplay().getMetrics(outMetrics);

        return outMetrics.widthPixels;
    }

    /**
     * 是否离线登录
     */
    public static boolean isLoginOffLine(Context context){
        if (PrefsUtil.getInt(context, CommonKey.ISONLINE) != 1){
            ToastUtils.show("离线登录，不可访问！");
            return true;
        }
        return false;
    }

    /**
     * 是否在线登录
     */
    public static boolean isLoginOnLine(Context context){
        if (PrefsUtil.getInt(context, CommonKey.ISONLINE) == 1){
            return true;
        }
        return false;
    }

    /**
     * 获取地图，通过登录状态
     */
    public static ArcGISMap getMapByLogin(Context context){
        ArcGISMap map = null;
        String isOnline = PrefsUtil.getString(context, CommonKey.ISONLINE);
        if (isOnline.equals("1")){
            map = new ArcGISMap(Basemap.Type.IMAGERY, SystemConfig.latitude, SystemConfig.longitude, SystemConfig.mapLevel);
        } else {
            String filePath = context.getExternalFilesDir(null).getAbsolutePath()+"/offline_map.tpk";
            Log.i(TAG,"离线文件路径："+filePath);
            TileCache tileCache = new TileCache(filePath);
            ArcGISTiledLayer tiledLayer = new ArcGISTiledLayer(tileCache);
            Basemap basemap = new Basemap(tiledLayer);
            map = new ArcGISMap(basemap);
        }
        return map;
    }

    public static List<ResourceTypeModel> getLocalData(Context context){
        String localJsonStr = getJson("ResourceTypeModel.json",context);
        JSONObject json = JSONObject.parseObject(localJsonStr);
        if (Integer.parseInt(json.get("code").toString()) == 1) {
            List<ResourceTypeModel> resultList = JSON.parseArray(json.getString("rst"), ResourceTypeModel.class);
            return resultList;
        } else{
            Log.e(TAG,"数据解析出错");
        }
        return null;
    }

    public static String getJson(String fileName,Context context) {
        //将json数据变成字符串
        StringBuilder stringBuilder = new StringBuilder();
        try {
            //获取assets资源管理器
            AssetManager assetManager = context.getAssets();
            //通过管理器打开文件并读取
            BufferedReader bf = new BufferedReader(new InputStreamReader(
                    assetManager.open(fileName)));
            String line;
            while ((line = bf.readLine()) != null) {
                stringBuilder.append(line);
            }
        } catch (IOException e) {
            e.printStackTrace();
        }
        return stringBuilder.toString();
    }

}
